import lodash from 'lodash';

export default class Modifieritem {
  constructor(inventoryOPtion) {
    this.id = inventoryOPtion.id;
    this.name = inventoryOPtion.name;
    this.isRequired = inventoryOPtion.isRequired;
    this.singleton = inventoryOPtion.singleton;
    this.options = [];
  }

  addOption(option) {
    this.options.push(option);
  }

  removeOptionAll() {
    //移出所有的
    this.options.splice(0, this.options.length);
  }

  removeOption(option) {
    //传入option id都可以
    let options = this.options;
    const id = option.id || option;
    for (var i = 0, l = this.options.length; i < l; i++) {
      if (options[i].id == id) {
        this.options.splice(i, 1);
        break;
      }
    }
  }

  reviseOption(optionIndex, option) {
    this.options[optionIndex] = option;
  }

  // getter&&setter
  get amount() {
    let price = 0;
    // debugger
    this.options.forEach(function(elem) {
      price += elem.price;
    });
    return price;
  }
  set amount(value) {
    return value;
  }

  toJSON() {
    return lodash.extend({}, lodash.clone(this), {
      amount: this.amount
    });
  }
}
